package com.wuda.sample;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import com.wuda.cfg.Configure;
import com.wuda.segmentation.GTSegmenter;
import com.wuda.segmentation.Token;
import com.wuda.util.CharacterTree;

public class SegmentTest {

	/**
	 * <p>
	 * 测试分词
	 * </p>
	 */
	public void seg(String filePath) {
		CharacterTree dic = new CharacterTree();
		long start = 0;
		long end = 0;
		long time = 0;
		try {
			start = System.currentTimeMillis();
			dic.load(Configure.DIC_RESOURCE);// 加载词库
			end = System.currentTimeMillis();
			System.out.println("加载词典用时：" + (end - start));
		} catch (IOException e) {
			e.printStackTrace();
		}
		String str = null;
		int length = 0;
		GTSegmenter xseg = new GTSegmenter(dic);
		List<Token> tokens = null;
		BufferedReader reader = null;
		while (length < 99000000) {
			try {
				reader = new BufferedReader(new FileReader(filePath));
				while ((str = reader.readLine()) != null) {
					start = System.currentTimeMillis();
					tokens = xseg.seg(str); // 执行分词
					end = System.currentTimeMillis();
					time += end - start;
					length += str.length();
					if (tokens == null) {
						continue;
					}
					for (Token tok : tokens) {
						String s = new String(tok.getCharArray());
						System.out.println(s);
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println("分词总时间：" + time + "ms,字符长度：" + length);

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SegmentTest test = new SegmentTest();
		test.seg("e:/wz.txt"); // 设置你的文章所在位置

	}

}
